﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;

public partial class admin_updateNguoiTimNguon : System.Web.UI.UserControl
{
#region "Member variables"
    int intID=0;
    private int intUserID = 0;
    private int intModuleID = 0;
    bool boolRead = false;
    bool boolWrite = false;
    bool boolUpdate = false;
    bool boolDelete = false;
    DANguoiTimNguon objNguoiTimNguon;
    DANguon objNguon;
#endregion
#region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
    #endregion
#region "Private methods"
    //Lay Nguon theo Nguoitimnguon
    private void loadNguon()
    {
        DataSet dsNguon = objNguon.LayDanhSachNguon();
        if (dsNguon.Tables[0].Rows != null && dsNguon.Tables[0].Rows.Count > 0)
        {
            this.ddlNguon.Items.Clear();
            foreach (DataRow dr in dsNguon.Tables[0].Rows)
            {
                ListItem liTemp = new ListItem();
                liTemp.Text = dr["Ten"].ToString();
                liTemp.Value = dr["ID"].ToString();
                this.ddlNguon.Items.Add(liTemp);                
            }
        }
    }
    //check validation
    private bool checkValidation()
    {        
        string strFullName = this.txtHoTen.Text.Trim();
        if (strFullName.Equals(""))
        {
            this.ctvFullName.IsValid = false;
            return false;
        }
        
        else
        {
            return true;
        }
    }
    private void LoadUpdate(int intID)
    {
        DataRow drNguoiTimNguon = objNguoiTimNguon.loadNguoiTimNguonID(intID);
        int intUser = Convert.ToInt32(drNguoiTimNguon["ID"].ToString().Trim());
        this.txtDiaChi.Text = drNguoiTimNguon["DiaChi"].ToString().Trim();
        this.txtDiDong.Text = drNguoiTimNguon["DiDong"].ToString().Trim();
        this.txtDienThoai.Text = drNguoiTimNguon["DienThoai"].ToString().Trim();
        this.txtEmail.Text = drNguoiTimNguon["Email"].ToString().Trim();
        this.txtFax.Text = drNguoiTimNguon["Fax"].ToString().Trim();
        this.txtGhiChu.Text = drNguoiTimNguon["GhiChu"].ToString().Trim();
        this.txtHoTen.Text = drNguoiTimNguon["HoTen"].ToString().Trim();
        bool strSex = Convert.ToBoolean(drNguoiTimNguon["GioiTinh"].ToString());  
        if (strSex)
        {
            this.ddlSex.Items[0].Selected = true;
        }
        else
        {
            this.ddlSex.Items[1].Selected = true;
        }
    }
    // Sua Address
    private void UpdateNguoiTimNguon()
    {        
        if (checkValidation())
        {
            int intid = Convert.ToInt32(this.hdfNguoiTimNguonID.Value);
            string strHoTen = this.txtHoTen.Text.Trim();
            string strDiaChi = this.txtDiaChi.Text.Trim();
            string strDienThoai = this.txtDienThoai.Text.Trim();
            string strEmail = this.txtEmail.Text.Trim();
            string strDiDong = this.txtDiDong.Text.Trim();
            string strFax = this.txtFax.Text.Trim();
            string strGhiChu = this.txtGhiChu.Text.Trim();
            bool Sex = Convert.ToBoolean(this.ddlSex.SelectedValue);
            int intNguonID = Convert.ToInt32(this.ddlNguon.SelectedValue);
            int intChẹck = objNguoiTimNguon.updateNguoiTimNguon(intid, strHoTen, strDiaChi, strDienThoai, strEmail,
                                    strDiDong, strFax, strGhiChu, Sex, intNguonID);
            if (intChẹck > 0)
            {
                this.lblMess.Text = "Thêm bản ghi thành công";
                resetFrom();
            }
            else if (intChẹck == 0)
            {
                this.lblMess.Text = "Bản ghi này đã có trong dữ liệu!";
            }
            else
            {
                this.lblMess.Text = "Lỗi trong quá trình thêm mới bản ghi!";
            }
        }
    }
    
    // Xóa Address
    private void DeleteAddress()
    {
        int intid = Convert.ToInt32(this.hdfNguoiTimNguonID.Value);
        int intChẹck = objNguoiTimNguon.removeNguoiTimNguon(intid);
        if (intChẹck > 0)
        {
            this.lblMess.Text = "Xóa thành công bản ghi";
        }
        else
        {
            this.lblMess.Text = "Lỗi trong quá trình xóa bản ghi!";
        }
    }
    //Reset text cua from    
    private void resetFrom()
    {
        this.txtDiaChi.Text = "";
        this.txtDiDong.Text = "";
        this.txtEmail.Text = "";
        this.txtFax.Text = "";
        this.txtGhiChu.Text = "";
        this.txtEmail.Text = "";
        this.txtHoTen.Text = "";
    }
#endregion

#region "Events handle"
    //Check quyen han su dung
    private void CheckUserModule(int UserID, int ModuleID)
    {
        
    }
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objNguoiTimNguon = new DANguoiTimNguon(sqlConnec);
        objNguon = new DANguon(sqlConnec);
    }
    //Main load
    protected void Page_Load(object sender, EventArgs e)
    {
            loadDataAccess();
            if (!Page.IsPostBack)
            {
                LoadUpdate(intID);
            }
            
    }   
     
    // Su kien dat lai form NguoiTim Nguon
    protected void btnReset_Click(object sender, EventArgs e)
    {
        resetFrom();
        this.lblMess.Text = "";
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        this.lblMess.Text = "";
        UpdateNguoiTimNguon();

    }
#endregion    
   
    
}
